package com.kool.kadmin.common.coding.codegen;


import com.kool.kadmin.common.coding.codegen.vo.ColumnMetaVo;
import com.kool.kadmin.common.constant.ErrorCode;
import com.kool.kadmin.common.exception.AppException;
import com.kool.kadmin.common.util.StringUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @author luyu
 */
public class ColumnsDao {
    private Connection connection = null;

    public ColumnsDao(Connection connection) {
        this.connection = connection;
    }

    /**
     * 查询mysql表结构
     *
     * @param condition
     * @return
     */
    public List<ColumnMetaVo> select(ColumnMetaVo condition) {
        if (null == condition) {
            throw new AppException(ErrorCode.SYSTEM_ERROR, StringUtil.getUUID());
        }
        List<ColumnMetaVo> listRet = new ArrayList<>();

        StringBuffer sbSql = new StringBuffer();
        sbSql.append("SELECT * FROM information_schema.COLUMNS ");
        sbSql.append(" WHERE 1=1 ");

        if (!StringUtil.isEmpty(condition.getTableName())) {
            sbSql.append(" AND TABLE_NAME = ? ");
        }
        if (!StringUtil.isEmpty(condition.getTableSchema())) {
            sbSql.append(" AND TABLE_SCHEMA = ? ");
        }

        sbSql.append(" order by ordinal_position");
        String sql = sbSql.toString();

        try {
            PreparedStatement state;
            state = connection.prepareStatement(sql);
            int i = 1;

            if (!StringUtil.isEmpty(condition.getTableName())) {
                state.setString(i, condition.getTableName());
                i++;
            }

            if (!StringUtil.isEmpty(condition.getTableSchema())) {
                state.setString(i, condition.getTableSchema());
                i++;
            }

            ResultSet rs = state.executeQuery();
            while (rs.next()) {
                ColumnMetaVo result = new ColumnMetaVo();
                result.setTableCatalog(rs.getString("TABLE_CATALOG"));
                result.setTableSchema(rs.getString("TABLE_SCHEMA"));
                result.setTableName(rs.getString("TABLE_NAME"));
                result.setColumnName(rs.getString("COLUMN_NAME"));
                result.setOrdinalPosition(rs.getDouble("ORDINAL_POSITION"));
                result.setColumnDefault(rs.getString("COLUMN_DEFAULT"));
                result.setIsNullable(rs.getString("IS_NULLABLE"));
                result.setDataType(rs.getString("DATA_TYPE"));
                result.setCharacterMaximumLength(rs
                        .getDouble("CHARACTER_MAXIMUM_LENGTH"));
                result.setCharacterOctetLength(rs
                        .getDouble("CHARACTER_OCTET_LENGTH"));
                result.setNumericPrecision(rs.getDouble("NUMERIC_PRECISION"));
                result.setNumericScale(rs.getDouble("NUMERIC_SCALE"));
                result.setCharacterSetName(rs.getString("CHARACTER_SET_NAME"));
                result.setCollationName(rs.getString("COLLATION_NAME"));
                result.setColumnType(rs.getString("COLUMN_TYPE"));
                result.setColumnKey(rs.getString("COLUMN_KEY"));
                result.setExtra(rs.getString("EXTRA"));
                result.setPrivileges(rs.getString("PRIVILEGES"));
                result.setColumnComment(rs.getString("COLUMN_COMMENT"));

                listRet.add(result);
            }
        } catch (Exception e) {
            throw new AppException(ErrorCode.SYSTEM_ERROR, StringUtil.getUUID());
        }
        return listRet;
    }
}
